package com.customview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.tools.R;


/**
 */
public class TimeView extends AppCompatTextView {
    private long hours;
    private long minutes;
    private long seconds;
    private long diff;
    private long days;

    public TimeView(Context context) {
        this(context, null);
    }

    public TimeView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TimeView);
        diff = a.getInteger(R.styleable.TimeView_time, 0) * 1000;
        //onCreate();
    }


    /**
     * 根据 attrs 设置时间开始
     */
//    private void onCreate() {
//        start();
//    }

    //开始计时
    private void start() {
        handler.removeMessages(1);
        getTime();
        Message message = handler.obtainMessage(1);
        handler.sendMessageDelayed(message, 1000);
    }

    public void log() {
        Log.d("TAG", "再打印  log/////////////////////////////////////////////////////////////////");
    }


    /**
     * 设置事件
     *
     * @param time
     */
    public void setTime(long time) {
        this.diff = time;
        start();
    }

    final Handler handler = new Handler(Looper.getMainLooper()) {
        @SuppressLint("ResourceAsColor")
        public void handleMessage(Message msg) {         // handle message
            Log.d("TAG", "再打印  handleMessage");
            switch (msg.what) {
                case 1:
                    diff = diff+1000;
//                    Message message = handler.obtainMessage(1);
//                    handler.sendMessageDelayed(message, 1000);
                    if (diff > 0) {
                        setVisibility(View.VISIBLE);
                        getShowTime();
                        Message message = handler.obtainMessage(1);
                        handler.sendMessageDelayed(message, 1000);
                    } else {
                        setVisibility(View.VISIBLE);
                        Message message = handler.obtainMessage(1);
                        handler.sendMessageDelayed(message, 1000);
                        setTextColor(R.color.colorPrimary);
                        getShowTime2();
                        // TODO: 2017/9/25 时间显示
//                        setVisibility(View.GONE);
                    }
                    break;

                default:
//                    Message message = handler.obtainMessage(2);
//                    handler.sendMessageDelayed(message, 1000);
                    handler.removeMessages(1);
                    setVisibility(View.GONE);
                    break;
            }
            Log.d("TAG", "再打印");
            super.handleMessage(msg);
        }
    };

    /**
     * 得到时间差
     */
    private void getTime() {
        try {
            days = diff / (1000 * 60 * 60 * 24);
            hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
            seconds = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / (1000);
            setText(days + ":" + hours + ":" + minutes + ":" + seconds);
        } catch (Exception e) {
        }
    }

    /**
     * 获得要显示的时间
     */
    private void getShowTime() {
        days = diff / (1000 * 60 * 60 * 24);
        hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
        seconds = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / (1000);
        setText(days + ":" + hours + ":" + minutes + ":" + seconds);
    }

    private void getShowTime2() {
        days = diff / (1000 * 60 * 60 * 24);
        hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
        seconds = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / (1000);
        setText("-"+ Math.abs(days) + ":" + Math.abs(hours) + ":" + Math.abs(minutes) + ":" + Math.abs(seconds));
    }

    /**
     * 以之前设置的时间重新开始
     */
    public void reStart() {
        this.diff = this.diff;
        start();
    }
//    public void reStart(int seconds) {
//        this.diff = this.time= seconds;
//        start();
//    }

    /**
     * 设置时间重新开始
     *
     * @param time 重新开始的事件
     */
    public void reStart(long time) {
        if (time > 0) {
            this.diff = time * 1000;
            Log.d("TAG", "+=========================" + diff);
        }else {
            // TODO: 2017/9/26
            this.diff = time * 1000;
        }
        start();
    }

    public void setMiss(){
//        Message message = handler.obtainMessage(2);
//        handler.sendMessageDelayed(message, 1000);
        handler.removeMessages(1);
        setVisibility(INVISIBLE);
    }
}
